﻿using Microsoft.Office.InfoPath;
using System.Xml;
using System.Xml.XPath;

namespace InfoPathHelpers
{
    public abstract class ContextBase
    {
        private FixedPositionNavigator _Root;

        public ContextBase(DataSource dataSource) : this(dataSource, null) { }

        public ContextBase(DataSource dataSource, IXmlNamespaceResolver namespaceManager)
        {
            Name = Name;
            NamespaceManager = namespaceManager;
            _Root = new FixedPositionNavigator(dataSource.CreateNavigator());
        }

        /// <summary>
        /// The name of the data source.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// An XPathRoot pointing to the root of the data source.
        /// </summary>
        public FixedPositionNavigator Root { get { return _Root; } }

        /// <summary>
        /// An XPathNavigator pointing to the root of the data source.
        /// </summary>
        public XPathNavigator Navigator { get { return _Root.Clone(); } }

        public IXmlNamespaceResolver NamespaceManager { get; private set; }

        public XPathNavigator SelectSingleNode(string xPath)
        {
            return (_Root.SelectSingleNode(xPath, NamespaceManager));
        }

        public XPathNodeIterator Select(string xPath)
        {
            return (_Root.Select(xPath, NamespaceManager));
        }
    }
}
